package edu.washington.ling.syntric.io;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.stanford.nlp.util.StringUtils;
import edu.washington.ling.syntric.util.XMLUtils;

public class ExampleDocWriterXML extends ExampleDocWriter {

	private Document doc;
	Element rootNode;
	
	public ExampleDocWriterXML(String path) throws IOException {
		super(path);
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			doc = docBuilder.newDocument();
			rootNode = doc.createElement("ExampleFile");
			doc.appendChild(rootNode);
		}
		catch(ParserConfigurationException e) {
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}

	@Override
	public void writeExample(Example exr) {
		writeExample(exr, super.allLayers);
	}
	@Override
	public void writeExample(Example exr, String[] includeLayers) {
		Element sentNode = doc.createElement("Sentence");
		sentNode.setAttribute("id", exr.getID());
		rootNode.appendChild(sentNode);
		if(exr.hasOriginal()) {
			Element origNode = doc.createElement("Original");
			origNode.setTextContent(exr.getOriginal());
			sentNode.appendChild(origNode);
		}
		if(exr.hasGloss()) {
			Element glossNode = doc.createElement("Gloss");
			glossNode.setTextContent(exr.getGloss());
			sentNode.appendChild(glossNode);
		}
		if(exr.hasTranslation()) {
			Element transNode = doc.createElement("Translation");
			transNode.setTextContent(exr.getTranslation());
			sentNode.appendChild(transNode);
		}
		Element treesNode = doc.createElement("Trees");
		sentNode.appendChild(treesNode);
		for (String layer: includeLayers) {
			if (exr.hasTreeRep(layer)) {
				TreeRepresentation tr = exr.getTreeRep(layer);
				Element treeNode = doc.createElement("Tree");
				treeNode.setAttribute("layer", tr.getLayer());
				treeNode.setAttribute("format", tr.getFormat());
				treeNode.setAttribute("metadata", StringUtils.join(tr.getMetaData(), ";"));
				String treeStr = StringUtils.join(tr.getTreeLines(), "\n");
				treeNode.setTextContent(treeStr);
				treesNode.appendChild(treeNode);
			}
		}
		Element systemTreesNode = doc.createElement("SystemTrees");
		sentNode.appendChild(systemTreesNode);
		for (TreeRepresentation tr: exr.getSystemTreeReps()) {
			Element treeNode = doc.createElement("SystemTree");
			treeNode.setAttribute("layer", tr.getLayer());
			treeNode.setAttribute("format", tr.getFormat());
			treeNode.setAttribute("metadata", StringUtils.join(tr.getMetaData(), ";"));
			String treeStr = StringUtils.join(tr.getTreeLines(), "\n");
			treeNode.setTextContent(treeStr);
			systemTreesNode.appendChild(treeNode);
		}
	}

	@Override
	public void close() {
		try {
			XMLUtils.writeXMLFile(doc, super.getPath());
		}
		catch(IOException e) {
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}

}
